home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxcpath.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  26.2 KB  |  930 lines

  1. /* Copyright (C) 1991, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxcpath.c,v 1.3 2000/09/19 19:00:35 lpd Exp $ */
  20. /* Implementation of clipping paths, other than actual clipping */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gsstruct.h"
  24. #include "gsutil.h"
  25. #include "gsline.h"
  26. #include "gxdevice.h"
  27. #include "gxfixed.h"
  28. #include "gscoord.h"        /* needs gsmatrix.h */
  29. #include "gxistate.h"
  30. #include "gzpath.h"
  31. #include "gzcpath.h"
  32.  
  33. /* Imported from gxacpath.c */
  34. extern int gx_cpath_intersect_path_slow(P4(gx_clip_path *, gx_path *, int,
  35.                        gs_imager_state *));
  36.  
  37. /* Forward references */
  38. private void gx_clip_list_from_rectangle(P2(gx_clip_list *, gs_fixed_rect *));
  39.  
  40. /* Other structure types */
  41. public_st_clip_rect();
  42. private_st_clip_list();
  43. public_st_clip_path();
  44. private_st_clip_rect_list();
  45. public_st_device_clip();
  46. private_st_cpath_enum();
  47.  
  48. /* GC procedures for gx_clip_path */
  49. private 
  50. ENUM_PTRS_WITH(clip_path_enum_ptrs, gx_clip_path *cptr) return ENUM_USING(st_path, &cptr->path, sizeof(cptr->path), index - 1);
  51.  
  52. case 0:
  53. ENUM_RETURN((cptr->rect_list == &cptr->local_list ? 0 :
  54.          cptr->rect_list));
  55. ENUM_PTRS_END
  56. private
  57. RELOC_PTRS_WITH(clip_path_reloc_ptrs, gx_clip_path *cptr)
  58. {
  59.     if (cptr->rect_list != &cptr->local_list)
  60.     RELOC_VAR(cptr->rect_list);
  61.     RELOC_USING(st_path, &cptr->path, sizeof(gx_path));
  62. }
  63. RELOC_PTRS_END
  64.  
  65. /* GC procedures for gx_device_clip */
  66. private
  67. ENUM_PTRS_WITH(device_clip_enum_ptrs, gx_device_clip *cptr)
  68. {
  69.     if (index < st_clip_list_max_ptrs + 1)
  70.     return ENUM_USING(st_clip_list, &cptr->list,
  71.               sizeof(gx_clip_list), index - 1);
  72.     return ENUM_USING(st_device_forward, vptr,
  73.               sizeof(gx_device_forward),
  74.               index - (st_clip_list_max_ptrs + 1));
  75. }
  76. case 0:
  77. ENUM_RETURN((cptr->current == &cptr->list.single ? NULL :
  78.          (void *)cptr->current));
  79. ENUM_PTRS_END
  80. private
  81. RELOC_PTRS_WITH(device_clip_reloc_ptrs, gx_device_clip *cptr)
  82. {
  83.     if (cptr->current == &cptr->list.single)
  84.     cptr->current = &((gx_device_clip *)RELOC_OBJ(vptr))->list.single;
  85.     else
  86.     RELOC_PTR(gx_device_clip, current);
  87.     RELOC_USING(st_clip_list, &cptr->list, sizeof(gx_clip_list));
  88.     RELOC_USING(st_device_forward, vptr, sizeof(gx_device_forward));
  89. }
  90. RELOC_PTRS_END
  91.  
  92. /* Define an empty clip list. */
  93. private const gx_clip_list clip_list_empty = {
  94.     {0, 0, min_int, max_int, 0, 0},
  95.     0, 0, 0, 0, 0
  96. };
  97.  
  98. /* ------ Clipping path memory management ------ */
  99.  
  100. private rc_free_proc(rc_free_cpath_list);
  101. private rc_free_proc(rc_free_cpath_list_local);
  102.  
  103. /*
  104.  * Initialize those parts of the contents of a clip path that aren't
  105.  * part of the path.
  106.  */
  107. private void
  108. cpath_init_rectangle(gx_clip_path * pcpath, gs_fixed_rect * pbox)
  109. {
  110.     gx_clip_list_from_rectangle(&pcpath->rect_list->list, pbox);
  111.     pcpath->inner_box = *pbox;
  112.     pcpath->path_valid = false;
  113.     pcpath->path.bbox = *pbox;
  114.     gx_cpath_set_outer_box(pcpath);
  115.     pcpath->id = gs_next_ids(1);    /* path changed => change id */
  116. }
  117. private void
  118. cpath_init_own_contents(gx_clip_path * pcpath)
  119. {    /* We could make null_rect static, but then it couldn't be const. */
  120.     gs_fixed_rect null_rect;
  121.  
  122.     null_rect.p.x = null_rect.p.y = null_rect.q.x = null_rect.q.y = 0;
  123.     cpath_init_rectangle(pcpath, &null_rect);
  124. }
  125. private void
  126. cpath_share_own_contents(gx_clip_path * pcpath, const gx_clip_path * shared)
  127. {
  128.     pcpath->inner_box = shared->inner_box;
  129.     pcpath->path_valid = shared->path_valid;
  130.     pcpath->outer_box = shared->outer_box;
  131.     pcpath->id = shared->id;
  132. }
  133.  
  134. /* Allocate only the segments of a clipping path on the heap. */
  135. private int
  136. cpath_alloc_list(gx_clip_rect_list ** prlist, gs_memory_t * mem,
  137.          client_name_t cname)
  138. {
  139.     rc_alloc_struct_1(*prlist, gx_clip_rect_list, &st_clip_rect_list, mem,
  140.               return_error(gs_error_VMerror), cname);
  141.     (*prlist)->rc.free = rc_free_cpath_list;
  142.     return 0;
  143. }
  144. int
  145. gx_cpath_init_contained_shared(gx_clip_path * pcpath,
  146.     const gx_clip_path * shared, gs_memory_t * mem, client_name_t cname)
  147. {
  148.     if (shared) {
  149.     if (shared->path.segments == &shared->path.local_segments) {
  150.         lprintf1("Attempt to share (local) segments of clip path 0x%lx!\n",
  151.              (ulong) shared);
  152.         return_error(gs_error_Fatal);
  153.     }
  154.     *pcpath = *shared;
  155.     pcpath->path.memory = mem;
  156.     pcpath->path.allocation = path_allocated_contained;
  157.     rc_increment(pcpath->path.segments);
  158.     rc_increment(pcpath->rect_list);
  159.     } else {
  160.     int code = cpath_alloc_list(&pcpath->rect_list, mem, cname);
  161.  
  162.     if (code < 0)
  163.         return code;
  164.     code = gx_path_alloc_contained(&pcpath->path, mem, cname);
  165.     if (code < 0) {
  166.         gs_free_object(mem, pcpath->rect_list, cname);
  167.         pcpath->rect_list = 0;
  168.         return code;
  169.     }
  170.     cpath_init_own_contents(pcpath);
  171.     }
  172.     return 0;
  173. }
  174. #define gx_cpath_alloc_contents(pcpath, shared, mem, cname)\
  175.   gx_cpath_init_contained_shared(pcpath, shared, mem, cname)
  176.  
  177. /* Allocate all of a clipping path on the heap. */
  178. gx_clip_path *
  179. gx_cpath_alloc_shared(const gx_clip_path * shared, gs_memory_t * mem,
  180.               client_name_t cname)
  181. {
  182.     gx_clip_path *pcpath =
  183.     gs_alloc_struct(mem, gx_clip_path, &st_clip_path, cname);
  184.     int code;
  185.  
  186.     if (pcpath == 0)
  187.     return 0;
  188.     code = gx_cpath_alloc_contents(pcpath, shared, mem, cname);
  189.     if (code < 0) {
  190.     gs_free_object(mem, pcpath, cname);
  191.     return 0;
  192.     }
  193.     pcpath->path.allocation = path_allocated_on_heap;
  194.     return pcpath;
  195. }
  196.  
  197. /* Initialize a stack-allocated clipping path. */
  198. int
  199. gx_cpath_init_local_shared(gx_clip_path * pcpath, const gx_clip_path * shared,
  200.                gs_memory_t * mem)
  201. {
  202.     if (shared) {
  203.     if (shared->path.segments == &shared->path.local_segments) {
  204.         lprintf1("Attempt to share (local) segments of clip path 0x%lx!\n",
  205.              (ulong) shared);
  206.         return_error(gs_error_Fatal);
  207.     }
  208.     pcpath->path = shared->path;
  209.     pcpath->path.allocation = path_allocated_on_stack;
  210.     rc_increment(pcpath->path.segments);
  211.     pcpath->rect_list = shared->rect_list;
  212.     rc_increment(pcpath->rect_list);
  213.     cpath_share_own_contents(pcpath, shared);
  214.     } else {
  215.     gx_path_init_local(&pcpath->path, mem);
  216.     rc_init_free(&pcpath->local_list, mem, 1, rc_free_cpath_list_local);
  217.     pcpath->rect_list = &pcpath->local_list;
  218.     cpath_init_own_contents(pcpath);
  219.     }
  220.     return 0;
  221. }
  222.  
  223. /* Unshare a clipping path. */
  224. int
  225. gx_cpath_unshare(gx_clip_path * pcpath)
  226. {
  227.     int code = gx_path_unshare(&pcpath->path);
  228.     gx_clip_rect_list *rlist = pcpath->rect_list;
  229.  
  230.     if (code < 0)
  231.     return code;
  232.     if (rlist->rc.ref_count > 1) {
  233.     int code = cpath_alloc_list(&pcpath->rect_list, pcpath->path.memory,
  234.                     "gx_cpath_unshare");
  235.  
  236.     if (code < 0)
  237.         return code;
  238.     /* Copy the rectangle list. */
  239. /**************** NYI ****************/
  240.     rc_decrement(rlist, "gx_cpath_unshare");
  241.     }
  242.     return code;
  243. }
  244.  
  245. /* Free a clipping path. */
  246. void
  247. gx_cpath_free(gx_clip_path * pcpath, client_name_t cname)
  248. {
  249.     rc_decrement(pcpath->rect_list, cname);
  250.     /* Clean up pointers for GC. */
  251.     pcpath->rect_list = 0;
  252.     {
  253.     gx_path_allocation_t alloc = pcpath->path.allocation;
  254.  
  255.     if (alloc == path_allocated_on_heap) {
  256.         pcpath->path.allocation = path_allocated_contained;
  257.         gx_path_free(&pcpath->path, cname);
  258.         gs_free_object(pcpath->path.memory, pcpath, cname);
  259.     } else
  260.         gx_path_free(&pcpath->path, cname);
  261.     }
  262. }
  263.  
  264. /* Assign a clipping path, preserving the source. */
  265. int
  266. gx_cpath_assign_preserve(gx_clip_path * pcpto, gx_clip_path * pcpfrom)
  267. {
  268.     int code = gx_path_assign_preserve(&pcpto->path, &pcpfrom->path);
  269.     gx_clip_rect_list *fromlist = pcpfrom->rect_list;
  270.     gx_clip_rect_list *tolist = pcpto->rect_list;
  271.     gx_path path;
  272.  
  273.     if (code < 0)
  274.     return 0;
  275.     if (fromlist == &pcpfrom->local_list) {
  276.     /* We can't use pcpfrom's list object. */
  277.     if (tolist == &pcpto->local_list || tolist->rc.ref_count > 1) {
  278.         /* We can't use pcpto's list either.  Allocate a new one. */
  279.         int code = cpath_alloc_list(&tolist, tolist->rc.memory,
  280.                     "gx_cpath_assign");
  281.  
  282.         if (code < 0)
  283.         return code;
  284.         rc_decrement(pcpto->rect_list, "gx_cpath_assign");
  285.     } else {
  286.         /* Use pcpto's list object. */
  287.         rc_free_cpath_list_local(tolist->rc.memory, tolist,
  288.                      "gx_cpath_assign");
  289.     }
  290.     tolist->list = fromlist->list;
  291.     pcpfrom->rect_list = tolist;
  292.     rc_increment(tolist);
  293.     } else {
  294.     /* We can use pcpfrom's list object. */
  295.     rc_increment(fromlist);
  296.     rc_decrement(pcpto->rect_list, "gx_cpath_assign");
  297.     }
  298.     path = pcpto->path, *pcpto = *pcpfrom, pcpto->path = path;
  299.     return 0;
  300. }
  301.  
  302. /* Assign a clipping path, releasing the source. */
  303. int
  304. gx_cpath_assign_free(gx_clip_path * pcpto, gx_clip_path * pcpfrom)
  305. {                /* For right now, just do assign + free. */
  306.     int code = gx_cpath_assign_preserve(pcpto, pcpfrom);
  307.  
  308.     if (code < 0)
  309.     return 0;
  310.     gx_cpath_free(pcpfrom, "gx_cpath_assign_free");
  311.     return 0;
  312. }
  313.  
  314. /* Free the clipping list when its reference count goes to zero. */
  315. private void
  316. rc_free_cpath_list_local(gs_memory_t * mem, void *vrlist,
  317.              client_name_t cname)
  318. {
  319.     gx_clip_rect_list *rlist = (gx_clip_rect_list *) vrlist;
  320.  
  321.     gx_clip_list_free(&rlist->list, mem);
  322. }
  323. private void
  324. rc_free_cpath_list(gs_memory_t * mem, void *vrlist, client_name_t cname)
  325. {
  326.     rc_free_cpath_list_local(mem, vrlist, cname);
  327.     gs_free_object(mem, vrlist, cname);
  328. }
  329.  
  330. /* ------ Clipping path accessing ------ */
  331.  
  332. /* Return the path of a clipping path. */
  333. int
  334. gx_cpath_to_path(gx_clip_path * pcpath, gx_path * ppath)
  335. {
  336.     if (!pcpath->path_valid) {
  337.     /* Synthesize a path. */
  338.     gs_cpath_enum cenum;
  339.     gs_fixed_point pts[3];
  340.     gx_path rpath;
  341.     int code;
  342.  
  343.     gx_path_init_local(&rpath, pcpath->path.memory);
  344.     gx_cpath_enum_init(&cenum, pcpath);
  345.     while ((code = gx_cpath_enum_next(&cenum, pts)) != 0) {
  346.         switch (code) {
  347.         case gs_pe_moveto:
  348.             code = gx_path_add_point(&rpath, pts[0].x, pts[0].y);
  349.             break;
  350.         case gs_pe_lineto:
  351.             code = gx_path_add_line_notes(&rpath, pts[0].x, pts[0].y,
  352.                            gx_cpath_enum_notes(&cenum));
  353.             break;
  354.         case gs_pe_curveto:
  355.             code = gx_path_add_curve_notes(&rpath, pts[0].x, pts[0].y,
  356.                            pts[1].x, pts[1].y,
  357.                            pts[2].x, pts[2].y,
  358.                            gx_cpath_enum_notes(&cenum));
  359.             break;
  360.         case gs_pe_closepath:
  361.             code = gx_path_close_subpath_notes(&rpath,
  362.                            gx_cpath_enum_notes(&cenum));
  363.             break;
  364.         default:
  365.             if (code >= 0)
  366.             code = gs_note_error(gs_error_unregistered);
  367.         }
  368.         if (code < 0)
  369.         break;
  370.     }
  371.     if (code >= 0)
  372.         code = gx_path_assign_free(&pcpath->path, &rpath);
  373.     if (code < 0) {
  374.         gx_path_free(&rpath, "gx_cpath_to_path error");
  375.         return code;
  376.     }
  377.     pcpath->path_valid = true;
  378.     }
  379.     return gx_path_assign_preserve(ppath, &pcpath->path);
  380. }
  381.  
  382. /* Return the inner and outer check rectangles for a clipping path. */
  383. /* Return true iff the path is a rectangle. */
  384. bool
  385. gx_cpath_inner_box(const gx_clip_path * pcpath, gs_fixed_rect * pbox)
  386. {
  387.     *pbox = pcpath->inner_box;
  388.     return clip_list_is_rectangle(gx_cpath_list(pcpath));
  389. }
  390. bool
  391. gx_cpath_outer_box(const gx_clip_path * pcpath, gs_fixed_rect * pbox)
  392. {
  393.     *pbox = pcpath->outer_box;
  394.     return clip_list_is_rectangle(gx_cpath_list(pcpath));
  395. }
  396.  
  397. /* Test if a clipping path includes a rectangle. */
  398. /* The rectangle need not be oriented correctly, i.e. x0 > x1 is OK. */
  399. bool
  400. gx_cpath_includes_rectangle(register const gx_clip_path * pcpath,
  401.                 fixed x0, fixed y0, fixed x1, fixed y1)
  402. {
  403.     return
  404.     (x0 <= x1 ?
  405.      (pcpath->inner_box.p.x <= x0 && x1 <= pcpath->inner_box.q.x) :
  406.      (pcpath->inner_box.p.x <= x1 && x0 <= pcpath->inner_box.q.x)) &&
  407.     (y0 <= y1 ?
  408.      (pcpath->inner_box.p.y <= y0 && y1 <= pcpath->inner_box.q.y) :
  409.      (pcpath->inner_box.p.y <= y1 && y0 <= pcpath->inner_box.q.y));
  410. }
  411.  
  412. /* Set the outer clipping box to the path bounding box, */
  413. /* expanded to pixel boundaries. */
  414. void
  415. gx_cpath_set_outer_box(gx_clip_path * pcpath)
  416. {
  417.     pcpath->outer_box.p.x = fixed_floor(pcpath->path.bbox.p.x);
  418.     pcpath->outer_box.p.y = fixed_floor(pcpath->path.bbox.p.y);
  419.     pcpath->outer_box.q.x = fixed_ceiling(pcpath->path.bbox.q.x);
  420.     pcpath->outer_box.q.y = fixed_ceiling(pcpath->path.bbox.q.y);
  421. }
  422.  
  423. /* Return the rectangle list of a clipping path (for local use only). */
  424. const gx_clip_list *
  425. gx_cpath_list(const gx_clip_path *pcpath)
  426. {
  427.     return &pcpath->rect_list->list;
  428. }
  429. /* Internal non-const version of the same accessor. */
  430. inline private gx_clip_list *
  431. gx_cpath_list_private(gx_clip_path *pcpath)
  432. {
  433.     return &pcpath->rect_list->list;
  434. }
  435.  
  436.  
  437. /* ------ Clipping path setting ------ */
  438.  
  439. /* Create a rectangular clipping path. */
  440. /* The supplied rectangle may not be oriented correctly, */
  441. /* but it will be oriented correctly upon return. */
  442. private int
  443. cpath_set_rectangle(gx_clip_path * pcpath, gs_fixed_rect * pbox)
  444. {
  445.     gx_clip_rect_list *rlist = pcpath->rect_list;
  446.  
  447.     if (rlist->rc.ref_count <= 1)
  448.     gx_clip_list_free(&rlist->list, rlist->rc.memory);
  449.     else {
  450.     int code = cpath_alloc_list(&pcpath->rect_list, pcpath->path.memory,
  451.                     "gx_cpath_from_rectangle");
  452.  
  453.     if (code < 0)
  454.         return code;
  455.     rc_decrement(rlist, "gx_cpath_from_rectangle");
  456.     rlist = pcpath->rect_list;
  457.     }
  458.     cpath_init_rectangle(pcpath, pbox);
  459.     return 0;
  460. }
  461. int
  462. gx_cpath_from_rectangle(gx_clip_path * pcpath, gs_fixed_rect * pbox)
  463. {
  464.     int code = gx_path_new(&pcpath->path);
  465.  
  466.     if (code < 0)
  467.     return code;
  468.     return cpath_set_rectangle(pcpath, pbox);
  469. }
  470. int
  471. gx_cpath_reset(gx_clip_path * pcpath)
  472. {
  473.     gs_fixed_rect null_rect;
  474.  
  475.     null_rect.p.x = null_rect.p.y = null_rect.q.x = null_rect.q.y = 0;
  476.     return gx_cpath_from_rectangle(pcpath, &null_rect);
  477. }
  478.  
  479. /* Intersect a new clipping path with an old one. */
  480. /* Flatten the new path first (in a copy) if necessary. */
  481. int
  482. gx_cpath_clip(gs_state *pgs, gx_clip_path *pcpath,
  483.           /*const*/ gx_path *ppath_orig, int rule)
  484. {
  485.     return gx_cpath_intersect(pcpath, ppath_orig, rule,
  486.                   (gs_imager_state *)pgs);
  487. }
  488. int
  489. gx_cpath_intersect(gx_clip_path *pcpath, /*const*/ gx_path *ppath_orig,
  490.            int rule, gs_imager_state *pis)
  491. {
  492.     gx_path fpath;
  493.     /*const*/ gx_path *ppath = ppath_orig;
  494.     gs_fixed_rect old_box, new_box;
  495.     int code;
  496.  
  497.     /* Flatten the path if necessary. */
  498.     if (gx_path_has_curves_inline(ppath)) {
  499.     gx_path_init_local(&fpath, pis->memory);
  500.     code = gx_path_add_flattened_accurate(ppath, &fpath,
  501.                           gs_currentflat_inline(pis),
  502.                           pis->accurate_curves);
  503.     if (code < 0)
  504.         return code;
  505.     ppath = &fpath;
  506.     }
  507.     /**************** SHOULD CHANGE THIS TO KEEP PATH ****************/
  508.     if (gx_cpath_inner_box(pcpath, &old_box) &&
  509.     ((code = gx_path_is_rectangle(ppath, &new_box)) ||
  510.      gx_path_is_void(ppath))
  511.     ) {
  512.     int changed = 0;
  513.  
  514.     if (!code) {
  515.         /* The new path is void. */
  516.         if (gx_path_current_point(ppath, &new_box.p) < 0) {
  517.         /* Use the user space origin (arbitrarily). */
  518.         new_box.p.x = float2fixed(pis->ctm.tx);
  519.         new_box.p.y = float2fixed(pis->ctm.ty);
  520.         }
  521.         new_box.q = new_box.p;
  522.         changed = 1;
  523.     } else {
  524.         /* Intersect the two rectangles if necessary. */
  525.         if (old_box.p.x > new_box.p.x)
  526.         new_box.p.x = old_box.p.x, ++changed;
  527.         if (old_box.p.y > new_box.p.y)
  528.         new_box.p.y = old_box.p.y, ++changed;
  529.         if (old_box.q.x < new_box.q.x)
  530.         new_box.q.x = old_box.q.x, ++changed;
  531.         if (old_box.q.y < new_box.q.y)
  532.         new_box.q.y = old_box.q.y, ++changed;
  533.         /* Check for a degenerate rectangle. */
  534.         if (new_box.q.x < new_box.p.x || new_box.q.y < new_box.p.y)
  535.         new_box.p = new_box.q, changed = 1;
  536.     }
  537.     if (changed == 4) {
  538.         /* The new box/path is the same as the old. */
  539.         return 0;
  540.     }
  541.     /* Release the existing path. */
  542.     gx_path_new(&pcpath->path);
  543.     ppath->bbox = new_box;
  544.     cpath_set_rectangle(pcpath, &new_box);
  545.     if (changed == 0) {
  546.         /* The path is valid; otherwise, defer constructing it. */
  547.         gx_path_assign_preserve(&pcpath->path, ppath);
  548.         pcpath->path_valid = true;
  549.     }
  550.     } else {
  551.     /* Existing clip path is not a rectangle.  Intersect the slow way. */
  552.     bool path_valid =
  553.         gx_cpath_inner_box(pcpath, &old_box) &&
  554.         gx_path_bbox(ppath, &new_box) >= 0 &&
  555.         gx_cpath_includes_rectangle(pcpath,
  556.                     new_box.p.x, new_box.p.y,
  557.                     new_box.q.x, new_box.q.y);
  558.  
  559.     code = gx_cpath_intersect_path_slow(pcpath, ppath, rule, pis);
  560.     if (code >= 0 && path_valid) {
  561.         gx_path_assign_preserve(&pcpath->path, ppath_orig);
  562.         pcpath->path_valid = true;
  563.     }
  564.     }
  565.     if (ppath != ppath_orig)
  566.     gx_path_free(ppath, "gx_cpath_clip");
  567.     return code;
  568. }
  569.  
  570. /* Scale a clipping path by a power of 2. */
  571. int
  572. gx_cpath_scale_exp2_shared(gx_clip_path * pcpath, int log2_scale_x,
  573.                int log2_scale_y, bool list_shared,
  574.                bool segments_shared)
  575. {
  576.     int code =
  577.     (pcpath->path_valid ?
  578.      gx_path_scale_exp2_shared(&pcpath->path, log2_scale_x, log2_scale_y,
  579.                    segments_shared) :
  580.      0);
  581.     gx_clip_list *list = gx_cpath_list_private(pcpath);
  582.     gx_clip_rect *pr;
  583.  
  584.     if (code < 0)
  585.     return code;
  586.     /* Scale the fixed entries. */
  587.     gx_rect_scale_exp2(&pcpath->inner_box, log2_scale_x, log2_scale_y);
  588.     gx_rect_scale_exp2(&pcpath->outer_box, log2_scale_x, log2_scale_y);
  589.     if (!list_shared) {
  590.     /* Scale the clipping list. */
  591.     pr = list->head;
  592.     if (pr == 0)
  593.         pr = &list->single;
  594.     for (; pr != 0; pr = pr->next)
  595.         if (pr != list->head && pr != list->tail) {
  596.  
  597. #define SCALE_V(v, s)\
  598.   if ( pr->v != min_int && pr->v != max_int )\
  599.     pr->v = (s >= 0 ? pr->v << s : pr->v >> -s)
  600.  
  601.         SCALE_V(xmin, log2_scale_x);
  602.         SCALE_V(xmax, log2_scale_x);
  603.         SCALE_V(ymin, log2_scale_y);
  604.         SCALE_V(ymax, log2_scale_y);
  605. #undef SCALE_V
  606.         }
  607.     }
  608.     pcpath->id = gs_next_ids(1);    /* path changed => change id */
  609.     return 0;
  610. }
  611.  
  612. /* ------ Clipping list routines ------ */
  613.  
  614. /* Initialize a clip list. */
  615. void
  616. gx_clip_list_init(gx_clip_list * clp)
  617. {
  618.     *clp = clip_list_empty;
  619. }
  620.  
  621. /* Initialize a clip list to a rectangle. */
  622. /* The supplied rectangle may not be oriented correctly, */
  623. /* but it will be oriented correctly upon return. */
  624. private void
  625. gx_clip_list_from_rectangle(register gx_clip_list * clp,
  626.                 register gs_fixed_rect * rp)
  627. {
  628.     gx_clip_list_init(clp);
  629.     if (rp->p.x > rp->q.x) {
  630.     fixed t = rp->p.x;
  631.  
  632.     rp->p.x = rp->q.x;
  633.     rp->q.x = t;
  634.     }
  635.     if (rp->p.y > rp->q.y) {
  636.     fixed t = rp->p.y;
  637.  
  638.     rp->p.y = rp->q.y;
  639.     rp->q.y = t;
  640.     }
  641.     clp->single.xmin = clp->xmin = fixed2int_var(rp->p.x);
  642.     clp->single.ymin = fixed2int_var(rp->p.y);
  643.     /* Handle degenerate rectangles specially. */
  644.     clp->single.xmax = clp->xmax =
  645.     (rp->q.x == rp->p.x ? clp->single.xmin :
  646.      fixed2int_var_ceiling(rp->q.x));
  647.     clp->single.ymax =
  648.     (rp->q.y == rp->p.y ? clp->single.ymin :
  649.      fixed2int_var_ceiling(rp->q.y));
  650.     clp->count = 1;
  651. }
  652.  
  653. /* Start enumerating a clipping path. */
  654. int
  655. gx_cpath_enum_init(gs_cpath_enum * penum, gx_clip_path * pcpath)
  656. {
  657.     if ((penum->using_path = pcpath->path_valid)) {
  658.     gx_path_enum_init(&penum->path_enum, &pcpath->path);
  659.     penum->rp = penum->visit = 0;
  660.     } else {
  661.     gx_path empty_path;
  662.     gx_clip_list *clp = gx_cpath_list_private(pcpath);
  663.     gx_clip_rect *head = (clp->count <= 1 ? &clp->single : clp->head);
  664.     gx_clip_rect *rp;
  665.  
  666.     /* Initialize the pointers in the path_enum properly. */
  667.     gx_path_init_local(&empty_path, pcpath->path.memory);
  668.     gx_path_enum_init(&penum->path_enum, &empty_path);
  669.     penum->visit = head;
  670.     for (rp = head; rp != 0; rp = rp->next)
  671.         rp->to_visit =
  672.         (rp->xmin < rp->xmax && rp->ymin < rp->ymax ?
  673.          visit_left | visit_right : 0);
  674.     penum->rp = 0;        /* scan will initialize */
  675.     penum->any_rectangles = false;
  676.     penum->state = cpe_scan;
  677.     penum->have_line = false;
  678.     }
  679.     return 0;
  680. }
  681.  
  682. /* Enumerate the next segment of a clipping path. */
  683. /* In general, this produces a path made up of zillions of tiny lines. */
  684. int
  685. gx_cpath_enum_next(gs_cpath_enum * penum, gs_fixed_point pts[3])
  686. {
  687.     if (penum->using_path)
  688.     return gx_path_enum_next(&penum->path_enum, pts);
  689. #define set_pt(xi, yi)\
  690.   (pts[0].x = int2fixed(xi), pts[0].y = int2fixed(yi))
  691. #define set_line(xi, yi)\
  692.   (penum->line_end.x = (xi), penum->line_end.y = (yi), penum->have_line = true)
  693.     if (penum->have_line) {
  694.     set_pt(penum->line_end.x, penum->line_end.y);
  695.     penum->have_line = false;
  696.     return gs_pe_lineto;
  697.     } {
  698.     gx_clip_rect *visit = penum->visit;
  699.     gx_clip_rect *rp = penum->rp;
  700.     cpe_visit_t first_visit = penum->first_visit;
  701.     cpe_state_t state = penum->state;
  702.     gx_clip_rect *look;
  703.     int code;
  704.  
  705.     switch (state) {
  706.  
  707.         case cpe_scan:
  708.         /* Look for the start of an edge to trace. */
  709.         for (; visit != 0; visit = visit->next) {
  710.             if (visit->to_visit & visit_left) {
  711.             set_pt(visit->xmin, visit->ymin);
  712.             first_visit = visit_left;
  713.             state = cpe_left;
  714.             } else if (visit->to_visit & visit_right) {
  715.             set_pt(visit->xmax, visit->ymax);
  716.             first_visit = visit_right;
  717.             state = cpe_right;
  718.             } else
  719.             continue;
  720.             rp = visit;
  721.             code = gs_pe_moveto;
  722.             penum->any_rectangles = true;
  723.             goto out;
  724.         }
  725.         /* We've enumerated all the edges. */
  726.         state = cpe_done;
  727.         if (!penum->any_rectangles) {
  728.             /* We didn't have any rectangles. */
  729.             set_pt(fixed_0, fixed_0);
  730.             code = gs_pe_moveto;
  731.             break;
  732.         }
  733.         /* falls through */
  734.  
  735.         case cpe_done:
  736.         /* All done. */
  737.         code = 0;
  738.         break;
  739.  
  740. /* We can't use the BEGIN ... END hack here: we need to be able to break. */
  741. #define return_line(px, py)\
  742.   set_pt(px, py); code = gs_pe_lineto; break
  743.  
  744.         case cpe_left:
  745.  
  746.           left:        /* Trace upward along a left edge. */
  747.         /* We're at the lower left corner of rp. */
  748.         rp->to_visit &= ~visit_left;
  749.         /* Look for an adjacent rectangle above rp. */
  750.         for (look = rp;
  751.              (look = look->next) != 0 &&
  752.              (look->ymin == rp->ymin ||
  753.               (look->ymin == rp->ymax && look->xmax <= rp->xmin));
  754.             );
  755.         /* Now we know look->ymin >= rp->ymax. */
  756.         if (look == 0 || look->ymin > rp->ymax ||
  757.             look->xmin >= rp->xmax
  758.             ) {        /* No adjacent rectangle, switch directions. */
  759.             state =
  760.             (rp == visit && first_visit == visit_right ? cpe_close :
  761.              (set_line(rp->xmax, rp->ymax), cpe_right));
  762.             return_line(rp->xmin, rp->ymax);
  763.         }
  764.         /* We found an adjacent rectangle. */
  765.         /* See if it also adjoins a rectangle to the left of rp. */
  766.         {
  767.             gx_clip_rect *prev = rp->prev;
  768.             gx_clip_rect *cur = rp;
  769.  
  770.             if (prev != 0 && prev->ymax == rp->ymax &&
  771.             look->xmin < prev->xmax
  772.             ) {    /* There's an adjoining rectangle as well. */
  773.             /* Switch directions. */
  774.             rp = prev;
  775.             state =
  776.                 (rp == visit && first_visit == visit_right ? cpe_close :
  777.                  (set_line(prev->xmax, prev->ymax), cpe_right));
  778.             return_line(cur->xmin, cur->ymax);
  779.             }
  780.             rp = look;
  781.             if (rp == visit && first_visit == visit_left)
  782.             state = cpe_close;
  783.             else if (rp->xmin == cur->xmin)
  784.             goto left;
  785.             else
  786.             set_line(rp->xmin, rp->ymin);
  787.             return_line(cur->xmin, cur->ymax);
  788.         }
  789.  
  790.         case cpe_right:
  791.  
  792.           right:        /* Trace downward along a right edge. */
  793.         /* We're at the upper right corner of rp. */
  794.         rp->to_visit &= ~visit_right;
  795.         /* Look for an adjacent rectangle below rp. */
  796.         for (look = rp;
  797.              (look = look->prev) != 0 &&
  798.              (look->ymax == rp->ymax ||
  799.               (look->ymax == rp->ymin && look->xmin >= rp->xmax));
  800.             );
  801.         /* Now we know look->ymax <= rp->ymin. */
  802.         if (look == 0 || look->ymax < rp->ymin ||
  803.             look->xmax <= rp->xmin
  804.             ) {        /* No adjacent rectangle, switch directions. */
  805.             state =
  806.             (rp == visit && first_visit == visit_left ? cpe_close :
  807.              (set_line(rp->xmin, rp->ymin), cpe_left));
  808.             return_line(rp->xmax, rp->ymin);
  809.         }
  810.         /* We found an adjacent rectangle. */
  811.         /* See if it also adjoins a rectangle to the right of rp. */
  812.         {
  813.             gx_clip_rect *next = rp->next;
  814.             gx_clip_rect *cur = rp;
  815.  
  816.             if (next != 0 && next->ymin == rp->ymin &&
  817.             look->xmax > next->xmin
  818.             ) {    /* There's an adjoining rectangle as well. */
  819.             /* Switch directions. */
  820.             rp = next;
  821.             state =
  822.                 (rp == visit && first_visit == visit_left ? cpe_close :
  823.                  (set_line(next->xmin, next->ymin), cpe_left));
  824.             return_line(cur->xmax, cur->ymin);
  825.             }
  826.             rp = look;
  827.             if (rp == visit && first_visit == visit_right)
  828.             state = cpe_close;
  829.             else if (rp->xmax == cur->xmax)
  830.             goto right;
  831.             else
  832.             set_line(rp->xmax, rp->ymax);
  833.             return_line(cur->xmax, cur->ymin);
  834.         }
  835.  
  836. #undef return_line
  837.  
  838.         case cpe_close:
  839.         /* We've gone all the way around an edge. */
  840.         code = gs_pe_closepath;
  841.         state = cpe_scan;
  842.         break;
  843.  
  844.         default:
  845.         return_error(gs_error_unknownerror);
  846.     }
  847.  
  848.       out:            /* Store the state before exiting. */
  849.     penum->visit = visit;
  850.     penum->rp = rp;
  851.     penum->first_visit = first_visit;
  852.     penum->state = state;
  853.     return code;
  854.     }
  855. #undef set_pt
  856. #undef set_line
  857. }
  858. segment_notes
  859. gx_cpath_enum_notes(const gs_cpath_enum * penum)
  860. {
  861.     return sn_none;
  862. }
  863.  
  864. /* Free a clip list. */
  865. void
  866. gx_clip_list_free(gx_clip_list * clp, gs_memory_t * mem)
  867. {
  868.     gx_clip_rect *rp = clp->tail;
  869.  
  870.     while (rp != 0) {
  871.     gx_clip_rect *prev = rp->prev;
  872.  
  873.     gs_free_object(mem, rp, "gx_clip_list_free");
  874.     rp = prev;
  875.     }
  876.     gx_clip_list_init(clp);
  877. }
  878.  
  879. /* ------ Debugging printout ------ */
  880.  
  881. #ifdef DEBUG
  882.  
  883. /* Print a clipping list. */
  884. void
  885. gx_clip_list_print(const gx_clip_list *list)
  886. {
  887.     const gx_clip_rect *pr;
  888.  
  889.     dlprintf3("   list count=%d xmin=%d xmax=%d\n",
  890.          list->count, list->xmin, list->xmax);
  891.     switch (list->count) {
  892.     case 0:
  893.         pr = 0;
  894.         break;
  895.     case 1:
  896.         pr = &list->single;
  897.         break;
  898.     default:
  899.         pr = list->head;
  900.     }
  901.     for (; pr != 0; pr = pr->next)
  902.     dlprintf4("   rect: (%d,%d),(%d,%d)\n",
  903.           pr->xmin, pr->ymin, pr->xmax, pr->ymax);
  904. }
  905.  
  906. /* Print a clipping path */
  907. void
  908. gx_cpath_print(const gx_clip_path * pcpath)
  909. {
  910.     if (pcpath->path_valid)
  911.     gx_path_print(&pcpath->path);
  912.     else
  913.     dlputs("   (path not valid)\n");
  914.     dlprintf4("   inner_box=(%g,%g),(%g,%g)\n",
  915.           fixed2float(pcpath->inner_box.p.x),
  916.           fixed2float(pcpath->inner_box.p.y),
  917.           fixed2float(pcpath->inner_box.q.x),
  918.           fixed2float(pcpath->inner_box.q.y));
  919.     dlprintf4("     outer_box=(%g,%g),(%g,%g)",
  920.           fixed2float(pcpath->outer_box.p.x),
  921.           fixed2float(pcpath->outer_box.p.y),
  922.           fixed2float(pcpath->outer_box.q.x),
  923.           fixed2float(pcpath->outer_box.q.y));
  924.     dprintf2("     rule=%d list.refct=%ld\n",
  925.          pcpath->rule, pcpath->rect_list->rc.ref_count);
  926.     gx_clip_list_print(gx_cpath_list(pcpath));
  927. }
  928.  
  929. #endif /* DEBUG */
  930.